package com.studentSystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.studentSystem.mapper.TeacherMapper;
import com.studentSystem.pojo.Teacher;
import com.studentSystem.service.TeacherService;
import com.studentSystem.utils.Md5Util;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.SnowflakeIdWorkerUtil;
import com.studentSystem.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Teacher findByPhoneNumber(String phoneNumber) {
        return teacherMapper.findByPhoneNumber(phoneNumber);
    }

    @Override
    public Teacher findById(Long id) {
        return teacherMapper.findById(id);
    }

    @Override
    public void register(String name, String password, String phoneNumber) {
        //唯一id
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        //加密
        String md5String = Md5Util.getMD5String(password);
        //添加
        teacherMapper.register(id, name, md5String, phoneNumber);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        teacherMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }

    @Override
    public List<Teacher> list() {
        List<Teacher> as = teacherMapper.list();
        if (as == null) return null;
        return as;
    }

    @Override
    public void update(Teacher teacher) {
        teacherMapper.update(teacher);
    }

}
